package com.bluepixel.tvitalia;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ChannelListAdapter extends BaseAdapter {
    private final Activity activity;
    private final ArrayList<HashMap<String, Object>> data;
    private static LayoutInflater inflater=null;

    public ChannelListAdapter(Activity a, ArrayList<HashMap<String, Object>> d) {
	activity=a;
	data=d;
	inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
	return data.size();
    }

    @Override
    public Object getItem(int position) {
	return position;
    }

    @Override
    public long getItemId(int position) {
	return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
	View vi=convertView;
	if (convertView == null) {
	    vi=inflater.inflate(R.layout.layout_channels_row, null, false);
	}

	TextView title=(TextView) vi.findViewById(R.id.channel_row_firstLine); // title
	TextView descr=(TextView) vi.findViewById(R.id.channel_row_secondLine); // descr
	ImageView thumb_image=(ImageView) vi.findViewById(R.id.channel_row_icon); // thumb image

	HashMap<String, Object> row=new HashMap<String, Object>();
	row=data.get(position);

	// Setting all values in listview
	title.setText((String) row.get(ChannelListActivity.KEY_TITLE));
	descr.setText((String) row.get(ChannelListActivity.KEY_DESC));
	thumb_image.setImageBitmap((Bitmap) row.get(ChannelListActivity.KEY_IMAGE));
	return vi;
    }
}
